﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ListFramework.Models;
using Microsoft.Phone.Controls;

namespace ListFramework
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Konstruktor
        public MainPage()
        {
            InitializeComponent();
            lbItems.ItemsSource = App.MainViewModel.Items.OrderBy(x => x.Value1);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            filterResults();
        }

        private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            filterResults();
        }

        private void filterResults()
        {
            switch (App.MainViewModel.SelectedFilterCriteria.FilterBehaviour)
            {
                case FilterBehaviour.Value1:
                    lbItems.ItemsSource =
                        App.MainViewModel.Items.Where(
                            x => x.Value1.ToLower().Contains(txtFilter.Text.ToLower())).OrderBy(x => x.Value1);
                    break;
                case FilterBehaviour.Value2:
                    lbItems.ItemsSource =
                        App.MainViewModel.Items.Where(
                            x => x.Value2.ToLower().Contains(txtFilter.Text.ToLower())).OrderBy(x => x.Value1);
                    break;
                case FilterBehaviour.Both:
                    lbItems.ItemsSource =
                        App.MainViewModel.Items.Where(
                            x => x.Value1.ToLower().Contains(txtFilter.Text.ToLower()) || x.Value2.ToLower().Contains(txtFilter.Text.ToLower())).OrderBy(x => x.Value1);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FilterSettingsPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}